﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;

namespace Wheeler.Common.Storage
{
    public class Imaging
    {
        /// <summary>
        /// Saves an In Memory Image object as jpeg in the specified path
        /// </summary>
        /// <param name="path"></param>
        /// <param name="img"></param>
        public static void SaveJpeg(string path, Image img)
        {
            var qualityParam = new EncoderParameter(Encoder.Quality, 100L);
            var jpegCodec = GetEncoderInfo("image/jpeg");

            var encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;
            img.Save(path, jpegCodec, encoderParams);
        }

        public static void Save(string path, Image img, ImageCodecInfo imageCodecInfo)
        {
            var qualityParam = new EncoderParameter(Encoder.Quality, 100L);

            var encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;
            img.Save(path, imageCodecInfo, encoderParams);
        }

        public static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            return ImageCodecInfo.GetImageEncoders().FirstOrDefault(t => t.MimeType == mimeType);
        }

        /// <summary>
        /// Resizes an Image Object with the specified width and height
        /// </summary>
        /// <param name="image"></param>
        /// <param name="newWidth"></param>
        /// <param name="maxHeight"></param>
        /// <param name="onlyResizeIfWider"></param>
        /// <returns></returns>
        public static Image Resize(Image image, int newWidth)
        {
            int newHeight = image.Height;
            if (image.Width > newWidth)
            {
                float originalAspectRatio = ((float)image.Width) / ((float)image.Height);
                newHeight = (int)((float)newWidth / originalAspectRatio); //image.Height * newWidth / image.Width;
            }
            else
                newWidth = image.Width;

            var thumbnail = new Bitmap(newWidth, newHeight);

            using (var graphic = Graphics.FromImage(thumbnail))
            {
                graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphic.SmoothingMode = SmoothingMode.HighQuality;
                graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                graphic.CompositingQuality = CompositingQuality.HighQuality;
                graphic.DrawImage(image, 0, 0, newWidth, newHeight);
            }
            //thumbnail.Dispose();
            return thumbnail;
        }

        public static Image Crop(Image img, Rectangle cropArea)
        {
            var bmpImage = new Bitmap(img);
            var bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
            bmpImage.Dispose();
            return bmpCrop;
        }
    }
}
